/**
 * Created by Jarek on 2019/1/18
 * 常用工具函数模块
 */
import Vue from 'vue'
import VM from '@/main'
import router from '@router'

export default {
  // 公用确认对话框
  uiConfirm (content = '确认消息!', {
    confirmButtonText = '确认',
    cancelButtonText = '取消',
    confirmButtonClass = '',
    cancelButtonClass = '',
    title = '温馨提示',
    confirmCallback,
    cancelCallback
  } = {}) {
    VM.$confirm(content, title, {
      confirmButtonText: confirmButtonText,
      cancelButtonText: cancelButtonText,
      cancelButtonClass: `${cancelButtonClass}`,
      confirmButtonClass: `${confirmButtonClass}`,
      center: true,
      showClose: false,
      closeOnClickModal: false
    }).then(() => {
      if (confirmCallback && typeof confirmCallback === 'function') {
        confirmCallback()
      }
    }).catch(() => {
      if (confirmCallback && typeof cancelCallback === 'function') {
        cancelCallback()
      }
    })
  },
  // 公用对话框
  uiDialog (content = '请输入提示内容', {
    title = '温馨提示',
    confirmButtonText = '确定',
    calFun
  } = {}) {
    VM.$alert(content, title, {
      confirmButtonText: confirmButtonText,
      center: true,
      showClose: false,
      callback: action => {
        if (calFun && typeof calFun === 'function') {
          calFun()
        }
      }
    })
  },
  // 加载转圈
  uiLoading (text = 'loading') {
    Vue.prototype.$loading({
      lock: true,
      text: text,
      spinner: 'el-icon-loading',
      background: 'rgba(255, 255, 255, 0.5)'
    })
  },
  // 关闭加载转圈
  uiHideLoading (text = 'loading') {
    Vue.prototype.$loading({
      lock: true,
      text: text,
      spinner: 'el-icon-loading',
      background: 'rgba(255, 255, 255, 0.3)'
    }).close()
  },
  /**
   * 公共界面路由 跳转
   * @param path 路由地址 -1 routePath
   * @param params 跳转的路由所带的参数
   * @param type 编程式导航类型 push replace go
   */
  psPageNext (path, {
    params = {},
    type = 'push'
  } = {}) {
    // 将参数存储在sessionStorage中
    if (Object.keys(params).length) {
      sessionStorage.setItem('_PARAMS_', JSON.stringify(params))
    }
    router[type](path)
  },
  /**
   * 获取参数值
   */
  params () {
    return JSON.parse(sessionStorage.getItem('_PARAMS_'))
  },
  /**
   * 封装Mock-ajax请求
   * @param transCode 交易码
   * @param params 上送参数
   * @param loading 是否请求时显示loading 转圈
   * @param needHandlerErr 是否需要插件的报错弹框 false：不用插件报错自行定义报错
   * @returns {Promise<any>}
   */
  mockPost (transCode, {
    params = {},
    showLoading = true,
    needHandlerErr = true
  } = {}) {
    return new Promise((resolve, reject) => {
      this.$csii.ajax(`/mock/${transCode}`, {
        showLoading: showLoading,
        needHandlerErr: needHandlerErr,
        params: params
      }).then((data) => {
        resolve(data)
      }).catch((res) => {
        reject(res)
      })
    })
  },
  /**
   * @param data 要保存session的数据
   * @returns {*}
   */
  setSessionObj (key, value) {
    sessionStorage.setItem(key, JSON.stringify(value))
  },
  /**
   * @param data 获取session存的数据
   * @returns {*}
   */
  getSessionObj (key) {
    return JSON.parse(sessionStorage.getItem(key))
  },
  /**
   * @param arr 需要映射的数组
   * @param arrkey 数组中的键名:key
   * @param key 传入的key
   * @param value 数组中的键值:value
   */
  dataMapping (arr, arrKey, key, value) {
    for (let i = 0; i < arr.length; i++) {
      if (arr[i][arrKey] === key) {
        return arr[i][value]
      }
    }
  }
}
